Ext.define('Seq.widget.Widget', {
    extend: 'Ext.Container',
    xtype: 'xwidget',

    defaultBindProperty: 'value',

    config: {
        data: {},
        value: '---',
        bcolor: 'transparent',
        color: 'good',
        format: '0.#',
        unit: '',
        text: '',
        icon: '',
        scale: 1,
        url: ''
    },

    data: {},

    listeners: {
        render(cmp, eOpts) {
            if (cmp.getUrl()) {
                Ext.fly(this.id).on('click', cmp.click, cmp);
            }
        }
    },

    colorOfValue(color, value) {
        let res = color;
        if (Ext.isArray(color)) {
            for (let item of color) {
                if (Ext.isObject(item)) {
                    if (value >= item.limit) {
                        res = item.color
                    }
                } else {
                    res = item;
                }
            }
        }
        return res;
    },

    num2str(value) {
        var me = this;
        if (me.config.lookups) {
            return me.config.lookups['v' + value];
        } else {
            return Ext.isNumeric(value) ? Ext.util.Format.number(value
                / me.config.scale, me.config.format) : Ext.isEmpty(value)
                ? '---'
                : value;
        }
    },

    setValue(value) {
        var me = this, data = me.getData();
        if (Ext.isArray(value)) {
            for (let v in value) {
                value[v] = me.num2str(value[v]);
            }
            value = value.join('/');
        } else {
            value = me.num2str(value);
        }
        data.value = value;
        me.setData(data);
    },

    setUnit(value) {
        var me = this, data = me.getData();
        data.unit = Ext.isEmpty(value) ? '' : `(${value})`;
        me.setData(data);
    },

    setText(value) {
        var me = this, data = me.getData();
        data.text = value;
        me.setData(data);
    },

    setColor(value) {
        var me = this, data = me.getData();
        data.color = value;
        me.setData(data);
        me.addCls(value); // cls不remove
    },

    setBcolor(value) {
        this.setStyle('background-color', value);
    },

    setUrl(value) {
        var me = this;
        if (!Ext.isEmpty(value)) {
            me.setStyle('cursor', 'pointer');
        }
        me.callParent(arguments);
    },

    setIcon(value) {
        var me = this, data = me.getData();
        data.icon = value;
        me.setData(data);
    },

    click() {
        let me = this;
        if (me.handler) {
            Ext.callback(me.handler, me.up('[controller]').getController(), [me, me.getUrl(), me.params])
        } else {
            Ext.getApplication().getMainView().getController().fireEvent('redirect', me.getUrl(), me.params);
        }
    }

})
